package DuoTai;

class Shape {//父类
    public void draw() {
        System.out.println("画图形");
    }
}
class Rect extends Shape {
    @Override
    public void draw() {
        System.out.println("矩形");
    }
}
class Cycle extends Shape {
    @Override
    public void draw() {
        System.out.println("圆");
    }
}
class Flower extends Shape {
    @Override
    public void draw() {
        super.draw();
    }
}
class Triangle extends Shape {
    @Override
    public void draw() {
        super.draw();
    }
}


public class DuoTai {//只有类要求在一个文件中有且只有一个被public修饰，类里面的方法可多个都被public修饰
    Cycle cycle = new Cycle();
    Rect rect = new Rect();
    Flower flower = new Flower();
    Triangle triangle = new Triangle();


    public void drawMap() {
        String[] strings = {"cycle","rect","cycle","rect","flower"};
        for (String s:strings) {
            if(s.equals("cycle")) {
                cycle.draw();
            }else if(s.equals("rect")) {
                rect.draw();
            } else if (s.equals("flower")) {
                flower.draw();
            }else {
                triangle.draw();
            }
        }
    }

    public static void drawMap2(Shape shape) {//传子类，父类形参接，向上转型
        shape.draw();//被重写方法
    }
    public static void main(String[] args) {
        Cycle cycle = new Cycle();
        Rect rect = new Rect();
        Flower flower = new Flower();
        Triangle triangle = new Triangle();
        drawMap2(cycle);
        drawMap2(rect);
        drawMap2(flower);
        drawMap2(triangle);
    }

    public void drawMap3() {
        //父类类变量数组接，赋值接转向上转型
        Shape[] shapes = {cycle,rect,cycle,rect,flower,triangle};
         //遍历来接收元素的变量，里面的元素已经向上转型了的
        for (Shape shape:shapes) {
            shape.draw();
        }
    }
}



